package org.dromara.patrol.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.patrol.domain.bo.PatrolTemplateBo;
import org.dromara.patrol.domain.vo.PatrolTemplateVo;
import org.dromara.patrol.service.IPatrolTemplateService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 巡店管理-巡店模板配置
 * 前端访问路由地址为:/patrol/template
 *
 * @author LionLi
 * @date 2024-03-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/template")
public class PatrolTemplateController extends BaseController {

    private final IPatrolTemplateService patrolTemplateService;

    /**
     * 查询巡店管理-巡店模板配置列表
     */
//    @SaCheckPermission("patrol:template:list")
    @GetMapping("/list")
    public TableDataInfo<PatrolTemplateVo> list(PatrolTemplateBo bo, PageQuery pageQuery) {
        return patrolTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出巡店管理-巡店模板配置列表
     */
//    @SaCheckPermission("system:template:export")
//    @Log(title = "巡店管理-巡店模板配置", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(PatrolTemplateBo bo, HttpServletResponse response) {
//        List<PatrolTemplateVo> list = patrolTemplateService.queryList(bo);
//        ExcelUtil.exportExcel(list, "巡店管理-巡店模板配置", PatrolTemplateVo.class, response);
//    }

    /**
     * 获取巡店管理-巡店模板配置详细信息
     *
     * @param templateId 主键
     */
//    @SaCheckPermission("patrol:template:query")
    @GetMapping("/{templateId}")
    public R<PatrolTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long templateId) {
        return R.ok(patrolTemplateService.queryById(templateId));
    }

    /**
     * 新增巡店管理-巡店模板配置
     */
    @SaCheckPermission("patrol:template:add")
    @Log(title = "巡店管理-巡店模板配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody PatrolTemplateBo bo) {
        //巡店的模版名称不能重复
        if (!patrolTemplateService.checkTemplateNameUnique(bo)) {
            return R.fail("新增模版'" + bo.getName() + "'失败，模版名称已存在");
        }
        return R.ok(patrolTemplateService.insertByBo(bo));
    }

    /**
     * 修改巡店管理-巡店模板配置
     */
    @SaCheckPermission("patrol:template:edit")
    @Log(title = "巡店管理-巡店模板配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PatrolTemplateBo bo) {
        //巡店的模版名称不能重复
        if (!patrolTemplateService.checkTemplateNameUnique(bo)) {
            return R.fail("修改模版'" + bo.getName() + "'失败，模版名称已存在");
        }
        return toAjax(patrolTemplateService.updateByBo(bo));
    }

    /**
     * 删除巡店管理-巡店模板配置
     *
     * @param templateIds 主键串
     */
    @SaCheckPermission("patrol:template:remove")
    @Log(title = "巡店管理-巡店模板配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] templateIds) {

        if (patrolTemplateService.checkTaskExist(templateIds)) {
            return R.warn("“有巡店任务使用该模板，不能删除”");
        }
        if (patrolTemplateService.checkReportExist(templateIds)) {
            return R.warn("有巡店报告关联该模板，不能删除");
        }
        return toAjax(patrolTemplateService.deleteWithValidByIds(List.of(templateIds), true));
    }
}
